package com.woniuxy.rabbit.kill.configuration;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * description: RabbitConfiguration <br>
 * date: 2022/1/15 3:32 下午 <br>
 * author: shesaifei <br>
 * version: 1.0 <br>
 */

@Configuration
public class RabbitConfiguration {

    /**
     * 业务消息队列、交换机
     */

    @Bean
    public Queue killQueue() {
        Map<String, Object> map = new HashMap<>();
        //设置死信交换机、死信路由
        map.put("x-dead-letter-exchange", "kill_dead_exchange");
        map.put("x-dead-letter-routing-key", "dead");

        //设置消息队列的长度
        map.put("x-max-length", 100);

        //设置消息益处怎么处理消息
        /**
         * 可选值：drop-head 删除头成为信
         *        reject-publish  直接决绝接收任何消息（生产者发送消息失败）
         */

        map.put("x-overflow", "drop-head");
        return new Queue("kill_queue", true, false, false, map);
    }

    @Bean
    public DirectExchange killExchange() {
        return new DirectExchange("kill_exchange");
    }

    @Bean
    public Binding bindingKillExchangeAndKillQueue(Queue killQueue, DirectExchange killExchange) {
        return BindingBuilder.bind(killQueue).to(killExchange).with("kill");
    }


    /**
     * 死信的消息队列、交换机
     */

    @Bean
    public Queue killDeadQueue() {

        return new Queue("kill_dead_queue");
    }

    @Bean
    public DirectExchange killDeadExchange() {
        return new DirectExchange("kill_dead_exchange");
    }

    @Bean
    public Binding bindingKillDeadQueueAndKillDeadExchange(Queue killDeadQueue, DirectExchange killDeadExchange) {
        return BindingBuilder.bind(killDeadQueue).to(killDeadExchange).with("dead");
    }
}
